package com.changgou.seckill.redislock;

import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class RedisSonLock {  //redis分布式锁 :redisson

    @Autowired
    private RedissonClient redissonClient;

    public void lock() {
        //获取锁对象:自定义的名字
        RLock lock1 = redissonClient.getLock("redisLock_lock1");
        //加锁  设置锁的过期时间
        lock1.lock(60, TimeUnit.SECONDS);
        //打印看当前的线程
        System.err.println("======lock======" + Thread.currentThread().getName());
    }

    //释放锁
    public void releas() {

        //获取释放锁的名字
        RLock lock1 = redissonClient.getLock("redisLock_lock1");
        //释放锁
        lock1.unlock();
        System.err.println("======unlock======" + Thread.currentThread().getName());
    }

}
